#import "MSScaleLoadingView.h"
#import "UIImage+OPUD.h"

@implementation MSScaleLoadingView

- (instancetype)initWithFrame:(CGRect)frame {
    if (CGRectEqualToRect(frame, CGRectZero)) {
        frame = CGRectMake(0, 0, 44, 44);
    }
    if (self = [super initWithFrame:frame]) {
        [self __setupUI];
        [self __addAnimation];
    }
    return self;
}

- (CGSize)intrinsicContentSize {
    return self.bounds.size;
}

- (void)__setupUI {
    [self addSubview:self.greenBackgroundImageView];
    [self addSubview:self.hudBackgroundView];
    [self.hudBackgroundView addSubview:self.hudImageView];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    // 计算中心点，只计算一次
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    // 统一配置 greenBackgroundImageView 布局
    CGFloat gW = 44;
    CGFloat gH = 44;
    self.greenBackgroundImageView.frame = CGRectMake(0, 0, gW, gH);
    self.greenBackgroundImageView.center = center;

    // 统一配置 hudBackgroundView 布局
    CGFloat hudWidth = CGRectGetWidth(self.bounds) * 11 / 16;
    self.hudBackgroundView.frame = CGRectMake(0, 0, hudWidth, hudWidth);
    self.hudBackgroundView.center = center;
    self.hudBackgroundView.layer.cornerRadius = hudWidth * 0.5;
    self.hudBackgroundView.layer.masksToBounds = YES;
    
    // 更新 hudImageView 布局
    CGFloat imageViewWidth = hudWidth * 9 / 11;
    CGFloat imageViewPadding = (hudWidth - imageViewWidth) / 2.0;
    self.hudImageView.frame = CGRectMake(imageViewPadding, imageViewPadding, imageViewWidth, imageViewWidth);
}

- (void)__addAnimation {
    // 旋转动画
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotationAnimation.toValue = @(M_PI * 2);
    rotationAnimation.duration = 0.8;
    rotationAnimation.repeatCount = HUGE_VALF;
    rotationAnimation.removedOnCompletion = NO;
    [self.greenBackgroundImageView.layer addAnimation:rotationAnimation forKey:@"rotation"];

    // 放大缩小动画
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.toValue = @(1.2);
    scaleAnimation.duration = 0.315;
    scaleAnimation.autoreverses = YES;
    scaleAnimation.repeatCount = HUGE_VALF;
    scaleAnimation.removedOnCompletion = NO;
    [self.hudBackgroundView.layer addAnimation:scaleAnimation forKey:@"scale"];
}

#pragma mark - Lazy Loading

- (UIImageView *)greenBackgroundImageView {
    if (!_greenBackgroundImageView) {
        _greenBackgroundImageView = [[UIImageView alloc] init];
        _greenBackgroundImageView.image = [UIImage opudImageNamed:@"op_loading_green_background"];
    }
    return _greenBackgroundImageView;
}

- (UIView *)hudBackgroundView {
    if (!_hudBackgroundView) {
        _hudBackgroundView = [[UIView alloc] init];
        _hudBackgroundView.backgroundColor = [UIColor whiteColor];
        
    }
    return _hudBackgroundView;
}

- (UIImageView *)hudImageView {
    if (!_hudImageView) {
        _hudImageView = [[UIImageView alloc] init];
        _hudImageView.image = [UIImage opudImageNamed:@"op_loading_green_opay"];
    }
    return _hudImageView;
}

- (void)dealloc {
    NSLog(@"MSHudLoadingView==========dealloc");
}

@end


